#include "widget.h"
#include "ui_widget.h"
#include "QShortcut"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //创建快捷键‘-’
    //创建快捷键对象
    QShortcut* cut1=new QShortcut(this);
    //给cut对象绑定一个快捷键
    cut1->setKey(QKeySequence('-'));
    //给cur对象连接上slider
    connect(cut1,&QShortcut::activated,this,&Widget::subSlider);

    //和上述方法一样，在创建一个+
    QShortcut* cut2=new QShortcut(this);
    //'='同等于'+'
    cut2->setKey(QKeySequence('='));
    connect(cut2,&QShortcut::activated,this,&Widget::addSlider);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::subSlider()
{
    int value=ui->horizontalSlider->value();
    if(value<ui->horizontalSlider->minimum())
        return;
    else
        ui->horizontalSlider->setValue(value-5);
}

void Widget::addSlider()
{
    int value=ui->horizontalSlider->value();
    if(value>ui->horizontalSlider->maximum())
        return;
    else
        ui->horizontalSlider->setValue(value+5);
}

//实时显示滑动条的值，显示在label上
void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText("当前值："+QString::number(value));
}
